<template>
  <div class="mod-demo-echarts">
    <el-alert
      title="健康生活，远离疾病！"
      type="warning"
      :closable="false">
      <div slot-scope="description">
        <p class="el-alert__description">l</p>
      </div>
    </el-alert>

    <el-row :gutter="20">
      <el-col :span="12">
        <el-card>
          <div id="J_chartPieBox" class="chart-box"></div>
        </el-card>
      </el-col>
      
    </el-row>
    <el-row  :gutter="20">
      <el-col :span="20">
        <el-card>
          <div id="J_chartBarBox" class="chart-box"></div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
  import echarts from 'echarts'
  export default {
    data () {
      return {
        chartPie: null,
        chartBar: null,
        dataList: [],
        dataList2: [],
        datas: []
        
      }
    },
    mounted () {
      this.getDataList()

      this.getDataList2()
      
      // this.initChartBar()
    },
    activated () {
      // 由于给echart添加了resize事件, 在组件激活时需要重新resize绘画一次, 否则出现空白bug
      if (this.chartPie) {
        this.chartPie.resize()
      }
       if (this.chartBar) {
        this.chartBar.resize()
      }
    },
    methods: {
      // 请求每种病床有人的数据、
      getDataList2() {
          this.$http({
          url: this.$http.adornUrl('/generator/room/analystype'),
          method: 'get'
        }).then(({data}) => {
          this.dataList2 = data.rowse;
          this.datas = this.dataList2.map(function (item){
            return item['bdnum'];
          })
         this.initChartBar();
        })
      },

      // 柱状图
      initChartBar () {
        var option = {
          tooltip: {
            trigger: 'axis',
            axisPointer: {
              type: 'shadow'
            }
          },
          legend: {
            data: ['慢病房', '特级双人房', '普通病房', '监护房房', '婴儿房']
          },
          grid: {
            left: '3%',
            right: '4%',
            bottom: '5%',
            containLabel: true
          },
          xAxis: [
            {
              type: 'category',
              data: ['有人', '空闲']
            }
          ],
          yAxis: [
            {
              type: 'value'
            }
          ],
          series: [
            {
              name: '慢病房',
              type: 'bar',
              data: [this.datas[0], this.datas[1]]
            },
            {
              name: '特级双人房',
              type: 'bar',
              data: [this.datas[3], this.datas[2]]
            },
            {
              name: '普通病房',
              type: 'bar',
              data: [this.datas[5], this.datas[4]]
            },
            {
              name: '监护病房',
              type: 'bar',
              data: [this.datas[6],this.datas[7]]
            },
            {
              name: '婴儿房',
              type: 'bar',
              data: [this.datas[9],this.datas[8]],
            }
            
          ]
        }
        this.chartBar = echarts.init(document.getElementById('J_chartBarBox'))
        this.chartBar.setOption(option)
        window.addEventListener('resize', () => {
          this.chartBar.resize()
        })
      },
      // 请求空床和有人的数据
      getDataList () {
        this.$http({
          url: this.$http.adornUrl('/generator/room/analys'),
          method: 'get'
        }).then(({data}) => {
          // console.log(data)
          this.dataList = data.rows
          // 渲染图形
          this.initChartPie()
        })
      },
      // 饼状图
      initChartPie () {
        var option = {
          backgroundColor: '#2c343c',
          title: {
            text: '疾病发病率',
            left: 'center',
            top: 20,
            textStyle: {
              color: '#ccc'
            }
          },
          tooltip: {
            trigger: 'item',
            formatter: '{a} <br/>{b} : {c} ({d}%)'
          },
          visualMap: {
            show: false,
            min: 600,
            max: 600,
            inRange: {
              colorLightness: [0, 1]
            }
          },
          series: [
            {
              name: '',
              type: 'pie',
              radius: '55%',
              center: ['50%', '50%'],
              data: this.dataList,
              roseType: 'radiu',
              label: {
                normal: {
                  textStyle: {
                    color: 'rgba(255, 255, 255, 0.3)'
                  }
                }
              },
              labelLine: {
                normal: {
                  lineStyle: {
                    color: 'rgba(255, 25, 255, 0.3)'
                  },
                  smooth: 0.2,
                  length: 10,
                  length2: 10
                }
              },
              itemStyle: {
                normal: {
                  color: '#c25939',
                  shadowBlur: 200,
                  shadowColor: 'rgba(0, 0, 0, 0.5)'
                }
              },
              animationType: 'scale',
              animationEasing: 'elasticOut'
              // animationDelay: function (idx) {
              //   return Math.random() * 200
              // }
            }
          ]
        }
        this.chartPie = echarts.init(document.getElementById('J_chartPieBox'))
        this.chartPie.setOption(option)
        window.addEventListener('resize', () => {
          this.chartPie.resize()
        })
      }
    }
  }
</script>

<style lang="scss">
  .mod-demo-echarts {
    > .el-alert {
      margin-bottom: 10px;
    }
    > .el-row {
      margin-top: -10px;
      margin-bottom: -10px;
      .el-col {
        padding-top: 10px;
        padding-bottom: 10px;
      }
    }
    .chart-box {
      min-height: 400px;
    }
  }
</style>
